package com.microlecture.service.impl;

import java.util.Map;

import org.apache.velocity.app.VelocityEngine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.microlecture.service.MailService;
import com.microlecture.service.SendMailService;


@Service
public class SendMailServiceImpl implements SendMailService {
	
	private static final Logger LOG = LoggerFactory.getLogger(SendMailServiceImpl.class);

	@Autowired
    private MailService mailService;

    @Autowired  
    VelocityEngine velocityEngine;

	@Override
	public boolean registerActivateCodeMail(String to, Map<String, Object> model) {
		String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "vm/registerActivateCode.vm", "UTF-8", model);
		try {
			mailService.sendHtmlMail(to, "【饭友网】 注册激活码", content);
			LOG.info("成功发送注册激活邮箱");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.info("发送注册激活邮件失败，请检查邮件地址");
			return false;
		}
	}

	/**
	 * 忘记密码激活码
	 * @param to
	 * @param model
	 * @return
	 */
	@Override
	public boolean activateCodeMail(String to, Map<String, Object> model) {
		String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "vm/activateCode.vm", "UTF-8", model);
		try {
			mailService.sendHtmlMail(to, "【饭友网】 重置密码激活码", content);
			LOG.info("成功发送忘记密码激活邮箱");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.info("发送忘记密码激活邮件失败，请检查邮件地址");
			return false;
		}
	}

	@Override
	public boolean registerTeacherMail(String to, Map<String, Object> model) {
		String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "vm/registerTeacher.vm", "UTF-8", model);
		try {
			mailService.sendHtmlMail(to, "【饭友网】 教师用户申请成功通知", content);
			LOG.info("成功发送忘记密码激活邮箱");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.info("发送忘记密码激活邮件失败，请检查邮件地址");
			return false;
		}
	}

	@Override
	public boolean updateTeacherMail(String to, Map<String, Object> model) {
		String content = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "vm/updateTeacher.vm", "UTF-8", model);
		try {
			mailService.sendHtmlMail(to, "【饭友网】 教师用户申请成功通知", content);
			LOG.info("成功发送忘记密码激活邮箱");
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.info("发送忘记密码激活邮件失败，请检查邮件地址");
			return false;
		}
	}

}
